package com.skp.shaphan.ui.dialogs;

import java.awt.Color;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;
import org.dyno.visual.swing.layouts.Trailing;

import com.skp.shaphan.actions.ExportDataAction;

/**
 * Dialog to allow the user to choose how to export their data.
 * @author Stephen Phillips
 *
 */
//VS4E -- DO NOT REMOVE THIS LINE!
public class DataExportDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JLabel jLabel0;
	private JRadioButton exportTypeClipboard;
	private JRadioButton exportTypeFile;
	private JComboBox formatType;
	private JLabel pathLabel;
	private JTextField exportPath;
	private JButton okButton;
	private JButton cancelButton;
	private ButtonGroup exportType;
	private JCheckBox includeHeadings;
	private static final String PREFERRED_LOOK_AND_FEEL = "javax.swing.plaf.metal.MetalLookAndFeel";
	public DataExportDialog() {
		initComponents();
	}

	public DataExportDialog(Frame parent) {
		super(parent);
		initComponents();
	}

	public DataExportDialog(Frame parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public DataExportDialog(Frame parent, String title) {
		super(parent, title);
		initComponents();
	}

	public DataExportDialog(Frame parent, String title, boolean modal) {
		super(parent, title, modal);
		initComponents();
	}

	public DataExportDialog(Frame parent, String title, boolean modal,
			GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		initComponents();
	}

	public DataExportDialog(Dialog parent) {
		super(parent);
		initComponents();
	}

	public DataExportDialog(Dialog parent, boolean modal) {
		super(parent, modal);
		initComponents();
	}

	public DataExportDialog(Dialog parent, String title) {
		super(parent, title);
		initComponents();
	}

	public DataExportDialog(Dialog parent, String title, boolean modal) {
		super(parent, title, modal);
		initComponents();
	}

	public DataExportDialog(Dialog parent, String title, boolean modal,
			GraphicsConfiguration arg) {
		super(parent, title, modal, arg);
		initComponents();
	}

	public DataExportDialog(Window parent) {
		super(parent);
		initComponents();
	}

	public DataExportDialog(Window parent, ModalityType modalityType) {
		super(parent, modalityType);
		initComponents();
	}

	public DataExportDialog(Window parent, String title) {
		super(parent, title);
		initComponents();
	}

	public DataExportDialog(Window parent, String title,
			ModalityType modalityType) {
		super(parent, title, modalityType);
		initComponents();
	}

	public DataExportDialog(Window parent, String title,
			ModalityType modalityType, GraphicsConfiguration arg) {
		super(parent, title, modalityType, arg);
		initComponents();
	}

	private void initComponents() {
		setTitle("Export");
		setFont(new Font("Dialog", Font.PLAIN, 12));
		setBackground(new Color(223, 223, 223));
		setForeground(Color.black);
		setLayout(new GroupLayout());
		add(getJLabel0(), new Constraints(new Leading(19, 87, 10, 10), new Leading(52, 10, 10)));
		add(getExportTypeClipboard(), new Constraints(new Leading(19, 8, 8), new Leading(21, 8, 8)));
		add(getExportTypeFile(), new Constraints(new Leading(153, 10, 10), new Leading(21, 8, 8)));
		add(getFormatType(), new Constraints(new Leading(85, 165, 10, 10), new Leading(49, 12, 12)));
		add(getOkButton(), new Constraints(new Trailing(12, 12, 12), new Trailing(12, 125, 125)));
		add(getCancelButton(), new Constraints(new Trailing(70, 12, 12), new Trailing(12, 125, 125)));
		add(getExportPath(), new Constraints(new Leading(85, 276, 12, 12), new Leading(81, 49, 49)));
		add(getPathLabel(), new Constraints(new Leading(21, 10, 10), new Leading(83, 12, 12)));
		add(getIncludeHeadings(), new Constraints(new Leading(85, 8, 8), new Leading(108, 8, 8)));
		initExportType();
		setSize(430, 193);
	}

	private JCheckBox getIncludeHeadings() {
		if (includeHeadings == null) {
			includeHeadings = new JCheckBox();
			includeHeadings.setText("include Headings");
		}
		return includeHeadings;
	}

	private void initExportType() {
		exportType = new ButtonGroup();
		exportType.add(getExportTypeFile());
		exportType.add(getExportTypeClipboard());
	}

	private void initButtonGroup1() {
		exportType = new ButtonGroup();
		exportType.add(getExportTypeFile());
		exportType.add(getExportTypeClipboard());
	}

	private JButton getCancelButton() {
		if (cancelButton == null) {
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					cancelButtonActionActionPerformed(event);
				}
			});
		}
		return cancelButton;
	}

	private JButton getOkButton() {
		if (okButton == null) {
			okButton = new JButton();
			okButton.setText("Ok");
			okButton.addActionListener(new ActionListener() {
	
				public void actionPerformed(ActionEvent event) {
					okButtonActionActionPerformed(event);
				}
			});
		}
		return okButton;
	}

	private JTextField getExportPath() {
		if (exportPath == null) {
			exportPath = new JTextField();
		}
		return exportPath;
	}

	private JLabel getPathLabel() {
		if (pathLabel == null) {
			pathLabel = new JLabel();
			pathLabel.setText("Path:");
		}
		return pathLabel;
	}

	private JComboBox getFormatType() {
		if (formatType == null) {
			formatType = new JComboBox();
			formatType.setModel(new DefaultComboBoxModel(new Object[] { "Tab Delimited", "Rich Text", "HTML", "SQL Insert Statements", "JSON" }));
			formatType.setDoubleBuffered(false);
			formatType.setBorder(null);
		}
		return formatType;
	}

	private JRadioButton getExportTypeFile() {
		if (exportTypeFile == null) {
			exportTypeFile = new JRadioButton();
			exportTypeFile.setText("File");
		}
		return exportTypeFile;
	}

	private JRadioButton getExportTypeClipboard() {
		if (exportTypeClipboard == null) {
			exportTypeClipboard = new JRadioButton();
			exportTypeClipboard.setSelected(true);
			exportTypeClipboard.setText("Clipboard");
		}
		return exportTypeClipboard;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			jLabel0.setText("Format:");
		}
		return jLabel0;
	}

	private static void installLnF() {
		try {
			String lnfClassname = PREFERRED_LOOK_AND_FEEL;
			if (lnfClassname == null)
				lnfClassname = UIManager.getCrossPlatformLookAndFeelClassName();
			UIManager.setLookAndFeel(lnfClassname);
		} catch (Exception e) {
			System.err.println("Cannot install " + PREFERRED_LOOK_AND_FEEL
					+ " on this platform:" + e.getMessage());
		}
	}

	/**
	 * Main entry of the class.
	 * Note: This class is only created so that you can easily preview the result at runtime.
	 * It is not expected to be managed by the designer.
	 * You can modify it as you like.
	 */
	public static void main(String[] args) {
		installLnF();
		SwingUtilities.invokeLater(new Runnable() {
			@Override
			public void run() {
				DataExportDialog dialog = new DataExportDialog();
				dialog
						.setDefaultCloseOperation(DataExportDialog.DISPOSE_ON_CLOSE);
				dialog.setTitle("DataExportDialog");
				dialog.setLocationRelativeTo(null);
				dialog.getContentPane().setPreferredSize(dialog.getSize());
				dialog.pack();
				dialog.setVisible(true);
			}
		});
	}

	private void okButtonActionActionPerformed(ActionEvent event) {
		ExportDataAction action = new ExportDataAction();
		if(exportTypeFile.isSelected()) {
			action.setExportType(ExportDataAction.EXPORT_TYPE_FILE);
		}
		if(exportTypeClipboard.isSelected()) {
			action.setExportType(ExportDataAction.EXPORT_TYPE_CLIPBOARD);
		}
		action.setExportPath(exportPath.getText());
		action.setIncludeHeadings(includeHeadings.isSelected());
		if(formatType.getSelectedIndex() == 0) {
			action.setFormat(ExportDataAction.FORMAT_TAB_DELIMITED);
		}
		if(formatType.getSelectedIndex() == 1) {
			action.setFormat(ExportDataAction.FORMAT_RTF);
		}
		if(formatType.getSelectedIndex() == 2) {
			action.setFormat(ExportDataAction.FORMAT_HTML);
		}
		if(formatType.getSelectedIndex() == 3) {
			action.setFormat(ExportDataAction.FORMAT_SQL_INSERT);
		}
		if(formatType.getSelectedIndex() == 4) {
			action.setFormat(ExportDataAction.FORMAT_JSON);
		}
		action.start();
		this.dispose();
	}

	private void cancelButtonActionActionPerformed(ActionEvent event) {
		this.dispose();
	}

}
